/**
 * @file eventhandler.c
 * @author ken (kangjiening@openvalley.net)
 * @brief 
 * @version 0.1
 * @date 2023-06-12
 * 
 * @copyright Copyright (c) 2023 湖南开鸿智谷数字产业有限公司
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-06-12 <td>v1.0     <td>ken     <td>内容
 * </table>
 */
*/

#include "config.h"
#include "eventrunner.h"
#include "innerevent.h"
#include "eventhandler.h"
typedef void (*CallbackType)(uint8_t *,uint8_t);
/**
 * @brief 
 * @param [in] id 
 * @return struct EventHandlerId* 
 * 
 * @details 
 */
struct EventHandlerId *EventHandlerCreate(struct EventRunnerId * id )
{
    struct EventHandlerId *eventHandlerId  = ( struct EventHandlerId *)malloc(szieof(eventHandlerId));
    if(eventHandlerId == NULL) return NULL;
    eventHandlerId->runnerId = id;
   struct InnerEventPool_S *pool =  InnerEventPoolCreate(MAX_EVENT_POOL_SIZE); 
   if(pool == NULL)
   {
     Event_free(eventHandlerId);
     eventHandlerId = NULL;
     return NULL;
   }
   eventHandlerId->innerEventPool = pool;
    return eventHandlerId;
}

/**
 * @brief 
 * @param [in] id 
 * @return int 
 * 
 * @details 
 */
int EventHandlerDelete(struct EventHandlerId * id )
{
    if(id != NULL)
    {
        Event_free(id);
        id = NULL;
        return 0;
    }
    if( InnerEventPoolDelete(id->innerEventPool) >= 0)
    {
        Event_free(id);
        return 0;
    }
    return -1;
}

/**
 * @brief 
 * @param [in] id 
 * @param [in] fun 
 * @param [in] args 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-12
 */
int EventHanlderPostImmediateTask(struct EventHandlerId * id, ExecFunction callback,PARAM_VAR_S var)
{
    InnerFunction_S innerFunction;
    InnerTimer_S innerTimer;
    if(id == NULL || callback == NULL ) return -1; 
    innerFunction.callback = (CallbackType)callback;
    if(var.data != NULL && var.len > 0)
    {
        if((innerFunction.param = Event_Malloc(sizeof(uint8_t)*(var.len+1))) == NULL)
        {
            return -1;
        }
        memset(innerFunction.param,0,sizeof(uint8_t)*(var.len+1));
        memcpy(innerFunction.param,var.data,var.len);
        innerFunction.len = var.len;
    }
    innerTimer.pri = IMMITED_PRIV;
    innerTimer.timeOut = 0;
    innerTimer.count = 0;
    struct InnerEvent_S* innerEvent = InnerEventCreateCallback(id->innerEventPool,innerFunPacket); 
    InnerEventCreateCallback(id->innerEventPool, IMMITEDTYPE,&innerFunction, &innerTimer);
    return 0;

   
}
/**
 * @brief 发送延时函数
 * @param [in] id 
 * @param [in] callback 
 * @param [in] param 
 * @param [in] timeOut 
 * @return int 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-13
 */
int EventHanlderPostDelayTask(struct EventHandlerId * id, void(*callback)(void *),void *param,uint332_t timeOut)
{
    if(id == NULL || callback == NULL ) return -1; 
     struct InnerFunPacket_S innerFunPacket;
    innerFunPacket.callback = callback;
    innerFunPacket.param = param;
    innerFunPacket.pri = LOW_PRIV;
    innerFunPacket.eventType = OVERTYPE;
    innerFunPacket.timeOut = timeOut;
    innerFunPacket.count = 0;
    struct InnerEvent_S* innerEvent = InnerEventCreateCallback(id->innerEventPool,innerFunPacket); /*这里需要优化 5个变量太多*/
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
}

/**
 * @brief 
 * @param [in] id 
 * @param [in] callback 
 * @param [in] param 
 * @param [in] timeOut  循环时间 会被挂载到对应的时钟槽上
 * @return int 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-13
 */
int  EventHanlderPostCycleTask(struct EventHandlerId * id, void(*callback)(void *),void *param,uint32_t timeOut,uint32_t count)
{
    if(id == NULL || callback == NULL ) return -1; 
    struct InnerFunPacket_S innerFunPacket;
    innerFunPacket.callback = callback;
    innerFunPacket.param = param;
    innerFunPacket.pri = IMMITED_PRIV;
    innerFunPacket.eventType = CIRCLETYPE;
    innerFunPacket.timeOut = timeOut;
    innerFunPacket.count = count;
    struct InnerEvent_S* innerEvent = InnerEventCreateCallback(id->innerEventPool,innerFunPacket); 
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
 
}


/**
 * @brief 
 * @param [in] id 
 * @param [in] callback 
 * @param [in] param 
 * @param [in] timeOut  超时时间,按照最小的时间刻度计算次数
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-13
 */
int EventHanlderPostTimingTask(struct EventHandlerId * id, void(*callback)(void *),void *param,uint32_t timeOut)
{
     if(id == NULL || callback == NULL ) return -1; 
    struct InnerFunPacket_S innerFunPacket;
    innerFunPacket.callback = callback;
    innerFunPacket.param = param;
    innerFunPacket.pri = IMMITED_PRIV;
    innerFunPacket.eventType = OVERTYPE;
    innerFunPacket.timeOut = timeOut;
    struct InnerEvent_S* innerEvent = InnerEventCreateCallback(id->innerEventPool,innerFunPacket); 
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
}

/**
 * @brief 发布主题，topic为数字
 * @param [in] id 
 * @param [in] topicNum 
 * @return int 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-13
 */
int EventHanlderPublicEventByNum(struct EventHandlerId * id,uint32_t topicNum)
{
    if(id == NULL) return -1;
    struct InnerEvent_S* innerEvent = InnerEventCreateNumber(id->innerEventPool, topicNum,PUBTYPE,NULL); 
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
    
}

/**
 * @brief  发布主题，topic为字符串
 * @param [in] id 
 * @param [in] topicString 
 * @return int 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-13
 */
int EventHanlderPublicEventByString(struct EventHandlerId * id,char *topicString)
{
    if(id == NULL || str == NULL ) return -1;
    struct InnerEvent_S* innerEvent = InnerEventCreateString(id->innerEventPool, topicString,PUBTYPE,NULL); 
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
}

/**
 * @brief 订阅主题
 * @param [in] id 
 * @param [in] topicNum 
 * @param [in] callback 
 * @param [in] param 
 * @return int 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-15
 */
int EventHanlderSubscribeEventByNum(struct EventHandlerId * id,uint32_t topicNum,void (*callback)(void))
{
    if(id == NULL) return -1;
    InnerFunction_S innerFunction;
    innerFunction.callback =(CallbackType)callback;
    innerFunction.param = NULL;
    innerFunction.len = 0;
    struct InnerEvent_S* innerEvent = InnerEventCreateNumber(id->innerEventPool, topicNum,SUBTYPE,&innerFunction); 
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
}

/**
 * @brief  订阅主题
 * @param [in] id 
 * @param [in] topicName 
 * @param [in] callback 
 * @param [in] param 
 * @return int 
 * 
 * @details 
 * @author ken (kangjiening@openvalley.net)
 * @date 2023-06-15
 */
int EventHanlderSubscribeEventByString(struct EventHandlerId * id,char* topicName,void (*callback)(viod))
{
    if(id == NULL) return -1;
    InnerFunction_S innerFunction;
    innerFunction.callback =(CallbackType)callback;
    innerFunction.param = NULL;
    innerFunction.len = 0;
    struct InnerEvent_S* innerEvent = InnerEventCreateString(id->innerEventPool, topicName,SUBTYPE,innerFunction); 
    if(EventQueueInsert(id->runnerId->queueId, innerEvent) < 0) return -1;
    return 0;
}


 IMMITEDTYPE = 0,
    CIRCLETYPE,
    OVERTYPE,
    PUBTYPE,
    SUBTYPE,
struct EventHandlerOps 
{
    int type;
    void (*callback)(void *param);
};
void EventHandlerExec(struct InnerEvent *innerEvent)
{
    if(innerEvent != NULL && innerEvent->callback != NULL)
    {
        innerEvent->callback(innerEvent->param);
    }
}
void EventHandlerWheelExec(struct InnerEvent *innerEvent)
{
    if(innerEvent != NULL && innerEvent->callback != NULL && )
}
struct EventHandlerOps gEventHandlerOps[]=
{
    {IMMITEDTYPE,EventHandlerExec},

};

void EventHandlerDistribute(struct InnerEvent *innerEvent)
{
    if(innerEvent == NULL) return;
    switch(innerEvent->EventType)
    {
         case IMMITEDTYPE:
         break;
         case CIRCLETYPE:
         break;
         case 
    }

}